<?php include '_init_.php';?>
<?php
$dtGift = new DataTable(constant('DB_TABLE_GIFT'));
$giftList = $dtGift->get_list('','name');
$selectedSizeList = array();
if($_REQUEST['addcostcall'])
{
	$selectedSizeList = explode(',',$_REQUEST['idlist']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/formvalidate.js"></script>
<script language="javascript">
/*
data structure
[{'gift_id':3,'gift_name':'2x4','number':5,'is_gift':'Y'},{'gift_id':5,'gift_name':'10x50','number':1,'is_gift':'N'}]
*/
$(function(){
	var info  = window.parent.dialog_function_get_gift_info();
	$.each(info,function(k,v){
		$('input[type=checkbox][name=ck_gift_id_'+v.gift_id+']').attr('checked',true);
		$('input[type=text][name=gift_number_'+v.gift_id+']').val(v.number);
	});
	<?php if($_REQUEST['addcostcall']):?>
	var isgift = window.parent.dialog_function_set_gift_get_isgift();
	if(isgift == 'Y')
	{
		$('input[name=isgift]').attr('checked',true)
	}
	<?php endif;?>
});
function setInfo()
{
	var info = [];
	$('input[type=checkbox][checked]').each(function(k,v){
	
		var name_attr = $(v).attr('name');
		var id = name_attr.substr(11);
		var numVal = $('input[type=text][name=gift_number_'+id+']').val();
		var giftName = $('input[type=hidden][name=gift_name_'+id+']').val();
		if(numVal > 0)
		{
		var item = {'gift_id':id,'gift_name':giftName,'number':numVal};
		<?php if($_REQUEST['addcostcall']):?>
		var is_gift = $('input[name=isgift]').attr('checked')?'Y':'N';
		item.is_gift = is_gift;
		<?php endif;?>
		info.push(item);
		}
	});
	window.parent.dialog_function_add_gift_callback(info);
}
</script>
</head>

<body>
<form method="post">
<input type="hidden" name="act" value="save"/>
<input type="hidden" name="id" value="<?php echo $info['id'];?>"/>
<div class="mainf-8" style="width:auto;">
  <ul>
  	    <?php if($_REQUEST['addcostcall']):?>
  	<li class="article-1e" style="width:400px;color: #990000;"><input type="checkbox" class="article-adminb-chbox" name="isgift">贈品 </li>
    <?php endif;?>
    <?php foreach($giftList as $tmpData):?>
        <?php
    	if($_REQUEST['addcostcall'] && in_array($tmpData['id'],$selectedSizeList))
    	{
    		continue;
    	}
    ?>
    <li class="article-1e">
    	 <input type="hidden" name="gift_name_<?php echo $tmpData['id'];?>" value="<?php echo $tmpData['name'];?>"/>
         <input type="checkbox" name="ck_gift_id_<?php echo $tmpData['id'];?>" />&nbsp; <?php echo $tmpData['name'];?> <input name="gift_number_<?php echo $tmpData['id'];?>" type="text"  class="maine-keying _validate_form_int" value=""/></li>
    <?php endforeach;?>
  </ul>

</div>
<div class="mainf-7" style="width:auto;">
<ul>
<li style="margin-left:500px;"><input name="" type="button" class="article-1a_btn" value="儲存" onclick="setInfo();"/></li>
</ul>
</div>
</form>
</body>
</html>
